# 采购订单相应功能
from models.user import User
from config import db, admin_address, contract_name, contract_address, contract_abi, redis_service
from results import *
from utils import common_utils,datetime_utils
from flask import jsonify

def create_procure_order(get_json):
    """_summary_ 创建采购订单
    """
    company_address = get_json['companyAddress']
    materials = get_json['materials']
    procure_time = get_json['procureTime'] # 解决将时间类型转换为整型
    procurementCycle = get_json['procurementCycle']
    pt_timestamp = datetime_utils.str_to_timestamp(procure_time) # 将字符串转变为时间戳
    
    # 调用智能合约中createProcureOrder
    # 触发此功能不能使用admin用户，应该使用采购公司的地址
    res = common_utils.common_bc_req(company_address, 
                                     contract_name, 
                                     'createProcureOrder', 
                                     [materials, pt_timestamp, procurementCycle], 
                                         contract_address,
                                         contract_abi)
    if res.status_code == 200:
        return success_result
    else:
        res_json = json.loads(res.text)
        return gen_result(500, res_json['message'])

def query_procure_order(get_json):
    company_address = get_json['companyAddress']
     
    # 1. 首先通过采购公司的地址获取公司详情
    res = common_utils.common_bc_req(admin_address, 
                                     contract_name, 
                                     'getProcueCompany', 
                                     [company_address], 
                                     contract_address,
                                     contract_abi)
    # 2. 通过公司详情中采购订单的索引号获取所有订单信息
    procure_index_list = json.loads(json.loads(res.text)[2])
    procure_value_list = []
    for index in procure_index_list:
        # 这时index为采购订单索引
        res = common_utils.common_bc_req(admin_address, 
                                     contract_name, 
                                     'getProcureOrder', 
                                     [index], 
                                     contract_address,
                                     contract_abi)
        r_list = json.loads(res.text)
        obj = {
            "index": index,
            "materials":r_list[0],
            "procureTime": datetime_utils.timestamp_to_str(int(r_list[1])),
            "procurementCycle":r_list[2],
            "procureCompany":r_list[3]
        }
        procure_value_list.append(obj)
    return gen_result(200, procure_value_list)


def query_procure_order_by_id(get_json):
    index = get_json['index']
    res = common_utils.common_bc_req(admin_address, 
                                     contract_name, 
                                     'getProcureOrder', 
                                     [index], 
                                     contract_address,
                                     contract_abi)
    r_list = json.loads(res.text)
    obj = {
            "index": index,
            "materials":r_list[0],
            "procureTime": datetime_utils.timestamp_to_str(int(r_list[1])),
            "procurementCycle":r_list[2],
            "procureCompany":r_list[3]
    }
    return gen_result(200, obj)